package com.itheima.reggie.controller;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 查询所有分类
    @GetMapping("/category/findAll")
    public ResultInfo findAll(){
        //调用service查询
        List<Category> list = categoryService.findAll();
        //返回结果
        return ResultInfo.success(list);
    }

    //分类新增
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category){//接收参数
        //调用service
        categoryService.save(category);
        //返回结果
        return ResultInfo.success();
    }

    //修改分类
    @PutMapping("/category")
    public ResultInfo update(@RequestBody Category category){
        //调用service
        categoryService.update(category);
        //返回结果
        return ResultInfo.success();
    }

    //删除分类
    @DeleteMapping("/category")
    public ResultInfo delete(Long id){
        //调用service删除
        categoryService.delete(id);
        // 返回结果
        return ResultInfo.success();
    }

    //新增菜品、套餐时查询的分类信息
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type){
        //2.根据type查询分类
        List<Category> categoryList = categoryService.findByType(type);
        //3.返回结果
        return ResultInfo.success(categoryList);
    }

}
